package com.xjgzinfo.usdp.web.rest;

import com.xjgzinfo.usdp.AvueServer4jApp;
import com.xjgzinfo.usdp.config.TestSecurityConfiguration;
import com.xjgzinfo.usdp.domain.UndoLog;
import com.xjgzinfo.usdp.repository.UndoLogRepository;
import com.xjgzinfo.usdp.service.UndoLogService;
import com.xjgzinfo.usdp.service.dto.UndoLogDTO;
import com.xjgzinfo.usdp.service.mapper.UndoLogMapper;
import com.xjgzinfo.usdp.service.dto.UndoLogCriteria;
import com.xjgzinfo.usdp.service.UndoLogQueryService;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;
import javax.persistence.EntityManager;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Integration tests for the {@link UndoLogResource} REST controller.
 */
@SpringBootTest(classes = { AvueServer4jApp.class, TestSecurityConfiguration.class })
@AutoConfigureMockMvc
@WithMockUser
public class UndoLogResourceIT {

    private static final Long DEFAULT_BRANCH_ID = 1L;
    private static final Long UPDATED_BRANCH_ID = 2L;
    private static final Long SMALLER_BRANCH_ID = 1L - 1L;

    private static final String DEFAULT_XID = "AAAAAAAAAA";
    private static final String UPDATED_XID = "BBBBBBBBBB";

    private static final String DEFAULT_CONTEXT = "AAAAAAAAAA";
    private static final String UPDATED_CONTEXT = "BBBBBBBBBB";

    private static final byte[] DEFAULT_ROLLBACK_INFO = TestUtil.createByteArray(1, "0");
    private static final byte[] UPDATED_ROLLBACK_INFO = TestUtil.createByteArray(1, "1");
    private static final String DEFAULT_ROLLBACK_INFO_CONTENT_TYPE = "image/jpg";
    private static final String UPDATED_ROLLBACK_INFO_CONTENT_TYPE = "image/png";

    private static final Boolean DEFAULT_LOG_STATUS = false;
    private static final Boolean UPDATED_LOG_STATUS = true;

    private static final Instant DEFAULT_LOG_CREATED = Instant.ofEpochMilli(0L);
    private static final Instant UPDATED_LOG_CREATED = Instant.now().truncatedTo(ChronoUnit.MILLIS);

    private static final Instant DEFAULT_LOG_MODIFIED = Instant.ofEpochMilli(0L);
    private static final Instant UPDATED_LOG_MODIFIED = Instant.now().truncatedTo(ChronoUnit.MILLIS);

    @Autowired
    private UndoLogRepository undoLogRepository;

    @Autowired
    private UndoLogMapper undoLogMapper;

    @Autowired
    private UndoLogService undoLogService;

    @Autowired
    private UndoLogQueryService undoLogQueryService;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restUndoLogMockMvc;

    private UndoLog undoLog;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static UndoLog createEntity(EntityManager em) {
        UndoLog undoLog = new UndoLog()
            .branchId(DEFAULT_BRANCH_ID)
            .xid(DEFAULT_XID)
            .context(DEFAULT_CONTEXT)
            .rollbackInfo(DEFAULT_ROLLBACK_INFO)
            .rollbackInfoContentType(DEFAULT_ROLLBACK_INFO_CONTENT_TYPE)
            .logStatus(DEFAULT_LOG_STATUS)
            .logCreated(DEFAULT_LOG_CREATED)
            .logModified(DEFAULT_LOG_MODIFIED);
        return undoLog;
    }
    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static UndoLog createUpdatedEntity(EntityManager em) {
        UndoLog undoLog = new UndoLog()
            .branchId(UPDATED_BRANCH_ID)
            .xid(UPDATED_XID)
            .context(UPDATED_CONTEXT)
            .rollbackInfo(UPDATED_ROLLBACK_INFO)
            .rollbackInfoContentType(UPDATED_ROLLBACK_INFO_CONTENT_TYPE)
            .logStatus(UPDATED_LOG_STATUS)
            .logCreated(UPDATED_LOG_CREATED)
            .logModified(UPDATED_LOG_MODIFIED);
        return undoLog;
    }

    @BeforeEach
    public void initTest() {
        undoLog = createEntity(em);
    }

    @Test
    @Transactional
    public void createUndoLog() throws Exception {
        int databaseSizeBeforeCreate = undoLogRepository.findAll().size();
        // Create the UndoLog
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(undoLog);
        restUndoLogMockMvc.perform(post("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isCreated());

        // Validate the UndoLog in the database
        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeCreate + 1);
        UndoLog testUndoLog = undoLogList.get(undoLogList.size() - 1);
        assertThat(testUndoLog.getBranchId()).isEqualTo(DEFAULT_BRANCH_ID);
        assertThat(testUndoLog.getXid()).isEqualTo(DEFAULT_XID);
        assertThat(testUndoLog.getContext()).isEqualTo(DEFAULT_CONTEXT);
        assertThat(testUndoLog.getRollbackInfo()).isEqualTo(DEFAULT_ROLLBACK_INFO);
        assertThat(testUndoLog.getRollbackInfoContentType()).isEqualTo(DEFAULT_ROLLBACK_INFO_CONTENT_TYPE);
        assertThat(testUndoLog.isLogStatus()).isEqualTo(DEFAULT_LOG_STATUS);
        assertThat(testUndoLog.getLogCreated()).isEqualTo(DEFAULT_LOG_CREATED);
        assertThat(testUndoLog.getLogModified()).isEqualTo(DEFAULT_LOG_MODIFIED);
    }

    @Test
    @Transactional
    public void createUndoLogWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = undoLogRepository.findAll().size();

        // Create the UndoLog with an existing ID
        undoLog.setId(1L);
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(undoLog);

        // An entity with an existing ID cannot be created, so this API call must fail
        restUndoLogMockMvc.perform(post("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isBadRequest());

        // Validate the UndoLog in the database
        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeCreate);
    }


    @Test
    @Transactional
    public void checkBranchIdIsRequired() throws Exception {
        int databaseSizeBeforeTest = undoLogRepository.findAll().size();
        // set the field null
        undoLog.setBranchId(null);

        // Create the UndoLog, which fails.
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(undoLog);


        restUndoLogMockMvc.perform(post("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isBadRequest());

        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkXidIsRequired() throws Exception {
        int databaseSizeBeforeTest = undoLogRepository.findAll().size();
        // set the field null
        undoLog.setXid(null);

        // Create the UndoLog, which fails.
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(undoLog);


        restUndoLogMockMvc.perform(post("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isBadRequest());

        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkContextIsRequired() throws Exception {
        int databaseSizeBeforeTest = undoLogRepository.findAll().size();
        // set the field null
        undoLog.setContext(null);

        // Create the UndoLog, which fails.
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(undoLog);


        restUndoLogMockMvc.perform(post("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isBadRequest());

        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkLogStatusIsRequired() throws Exception {
        int databaseSizeBeforeTest = undoLogRepository.findAll().size();
        // set the field null
        undoLog.setLogStatus(null);

        // Create the UndoLog, which fails.
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(undoLog);


        restUndoLogMockMvc.perform(post("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isBadRequest());

        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkLogCreatedIsRequired() throws Exception {
        int databaseSizeBeforeTest = undoLogRepository.findAll().size();
        // set the field null
        undoLog.setLogCreated(null);

        // Create the UndoLog, which fails.
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(undoLog);


        restUndoLogMockMvc.perform(post("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isBadRequest());

        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void checkLogModifiedIsRequired() throws Exception {
        int databaseSizeBeforeTest = undoLogRepository.findAll().size();
        // set the field null
        undoLog.setLogModified(null);

        // Create the UndoLog, which fails.
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(undoLog);


        restUndoLogMockMvc.perform(post("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isBadRequest());

        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    public void getAllUndoLogs() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList
        restUndoLogMockMvc.perform(get("/api/undo-logs?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(undoLog.getId().intValue())))
            .andExpect(jsonPath("$.[*].branchId").value(hasItem(DEFAULT_BRANCH_ID.intValue())))
            .andExpect(jsonPath("$.[*].xid").value(hasItem(DEFAULT_XID)))
            .andExpect(jsonPath("$.[*].context").value(hasItem(DEFAULT_CONTEXT)))
            .andExpect(jsonPath("$.[*].rollbackInfoContentType").value(hasItem(DEFAULT_ROLLBACK_INFO_CONTENT_TYPE)))
            .andExpect(jsonPath("$.[*].rollbackInfo").value(hasItem(Base64Utils.encodeToString(DEFAULT_ROLLBACK_INFO))))
            .andExpect(jsonPath("$.[*].logStatus").value(hasItem(DEFAULT_LOG_STATUS.booleanValue())))
            .andExpect(jsonPath("$.[*].logCreated").value(hasItem(DEFAULT_LOG_CREATED.toString())))
            .andExpect(jsonPath("$.[*].logModified").value(hasItem(DEFAULT_LOG_MODIFIED.toString())));
    }
    
    @Test
    @Transactional
    public void getUndoLog() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get the undoLog
        restUndoLogMockMvc.perform(get("/api/undo-logs/{id}", undoLog.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(undoLog.getId().intValue()))
            .andExpect(jsonPath("$.branchId").value(DEFAULT_BRANCH_ID.intValue()))
            .andExpect(jsonPath("$.xid").value(DEFAULT_XID))
            .andExpect(jsonPath("$.context").value(DEFAULT_CONTEXT))
            .andExpect(jsonPath("$.rollbackInfoContentType").value(DEFAULT_ROLLBACK_INFO_CONTENT_TYPE))
            .andExpect(jsonPath("$.rollbackInfo").value(Base64Utils.encodeToString(DEFAULT_ROLLBACK_INFO)))
            .andExpect(jsonPath("$.logStatus").value(DEFAULT_LOG_STATUS.booleanValue()))
            .andExpect(jsonPath("$.logCreated").value(DEFAULT_LOG_CREATED.toString()))
            .andExpect(jsonPath("$.logModified").value(DEFAULT_LOG_MODIFIED.toString()));
    }


    @Test
    @Transactional
    public void getUndoLogsByIdFiltering() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        Long id = undoLog.getId();

        defaultUndoLogShouldBeFound("id.equals=" + id);
        defaultUndoLogShouldNotBeFound("id.notEquals=" + id);

        defaultUndoLogShouldBeFound("id.greaterThanOrEqual=" + id);
        defaultUndoLogShouldNotBeFound("id.greaterThan=" + id);

        defaultUndoLogShouldBeFound("id.lessThanOrEqual=" + id);
        defaultUndoLogShouldNotBeFound("id.lessThan=" + id);
    }


    @Test
    @Transactional
    public void getAllUndoLogsByBranchIdIsEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where branchId equals to DEFAULT_BRANCH_ID
        defaultUndoLogShouldBeFound("branchId.equals=" + DEFAULT_BRANCH_ID);

        // Get all the undoLogList where branchId equals to UPDATED_BRANCH_ID
        defaultUndoLogShouldNotBeFound("branchId.equals=" + UPDATED_BRANCH_ID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByBranchIdIsNotEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where branchId not equals to DEFAULT_BRANCH_ID
        defaultUndoLogShouldNotBeFound("branchId.notEquals=" + DEFAULT_BRANCH_ID);

        // Get all the undoLogList where branchId not equals to UPDATED_BRANCH_ID
        defaultUndoLogShouldBeFound("branchId.notEquals=" + UPDATED_BRANCH_ID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByBranchIdIsInShouldWork() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where branchId in DEFAULT_BRANCH_ID or UPDATED_BRANCH_ID
        defaultUndoLogShouldBeFound("branchId.in=" + DEFAULT_BRANCH_ID + "," + UPDATED_BRANCH_ID);

        // Get all the undoLogList where branchId equals to UPDATED_BRANCH_ID
        defaultUndoLogShouldNotBeFound("branchId.in=" + UPDATED_BRANCH_ID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByBranchIdIsNullOrNotNull() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where branchId is not null
        defaultUndoLogShouldBeFound("branchId.specified=true");

        // Get all the undoLogList where branchId is null
        defaultUndoLogShouldNotBeFound("branchId.specified=false");
    }

    @Test
    @Transactional
    public void getAllUndoLogsByBranchIdIsGreaterThanOrEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where branchId is greater than or equal to DEFAULT_BRANCH_ID
        defaultUndoLogShouldBeFound("branchId.greaterThanOrEqual=" + DEFAULT_BRANCH_ID);

        // Get all the undoLogList where branchId is greater than or equal to UPDATED_BRANCH_ID
        defaultUndoLogShouldNotBeFound("branchId.greaterThanOrEqual=" + UPDATED_BRANCH_ID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByBranchIdIsLessThanOrEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where branchId is less than or equal to DEFAULT_BRANCH_ID
        defaultUndoLogShouldBeFound("branchId.lessThanOrEqual=" + DEFAULT_BRANCH_ID);

        // Get all the undoLogList where branchId is less than or equal to SMALLER_BRANCH_ID
        defaultUndoLogShouldNotBeFound("branchId.lessThanOrEqual=" + SMALLER_BRANCH_ID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByBranchIdIsLessThanSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where branchId is less than DEFAULT_BRANCH_ID
        defaultUndoLogShouldNotBeFound("branchId.lessThan=" + DEFAULT_BRANCH_ID);

        // Get all the undoLogList where branchId is less than UPDATED_BRANCH_ID
        defaultUndoLogShouldBeFound("branchId.lessThan=" + UPDATED_BRANCH_ID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByBranchIdIsGreaterThanSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where branchId is greater than DEFAULT_BRANCH_ID
        defaultUndoLogShouldNotBeFound("branchId.greaterThan=" + DEFAULT_BRANCH_ID);

        // Get all the undoLogList where branchId is greater than SMALLER_BRANCH_ID
        defaultUndoLogShouldBeFound("branchId.greaterThan=" + SMALLER_BRANCH_ID);
    }


    @Test
    @Transactional
    public void getAllUndoLogsByXidIsEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where xid equals to DEFAULT_XID
        defaultUndoLogShouldBeFound("xid.equals=" + DEFAULT_XID);

        // Get all the undoLogList where xid equals to UPDATED_XID
        defaultUndoLogShouldNotBeFound("xid.equals=" + UPDATED_XID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByXidIsNotEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where xid not equals to DEFAULT_XID
        defaultUndoLogShouldNotBeFound("xid.notEquals=" + DEFAULT_XID);

        // Get all the undoLogList where xid not equals to UPDATED_XID
        defaultUndoLogShouldBeFound("xid.notEquals=" + UPDATED_XID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByXidIsInShouldWork() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where xid in DEFAULT_XID or UPDATED_XID
        defaultUndoLogShouldBeFound("xid.in=" + DEFAULT_XID + "," + UPDATED_XID);

        // Get all the undoLogList where xid equals to UPDATED_XID
        defaultUndoLogShouldNotBeFound("xid.in=" + UPDATED_XID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByXidIsNullOrNotNull() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where xid is not null
        defaultUndoLogShouldBeFound("xid.specified=true");

        // Get all the undoLogList where xid is null
        defaultUndoLogShouldNotBeFound("xid.specified=false");
    }
                @Test
    @Transactional
    public void getAllUndoLogsByXidContainsSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where xid contains DEFAULT_XID
        defaultUndoLogShouldBeFound("xid.contains=" + DEFAULT_XID);

        // Get all the undoLogList where xid contains UPDATED_XID
        defaultUndoLogShouldNotBeFound("xid.contains=" + UPDATED_XID);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByXidNotContainsSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where xid does not contain DEFAULT_XID
        defaultUndoLogShouldNotBeFound("xid.doesNotContain=" + DEFAULT_XID);

        // Get all the undoLogList where xid does not contain UPDATED_XID
        defaultUndoLogShouldBeFound("xid.doesNotContain=" + UPDATED_XID);
    }


    @Test
    @Transactional
    public void getAllUndoLogsByContextIsEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where context equals to DEFAULT_CONTEXT
        defaultUndoLogShouldBeFound("context.equals=" + DEFAULT_CONTEXT);

        // Get all the undoLogList where context equals to UPDATED_CONTEXT
        defaultUndoLogShouldNotBeFound("context.equals=" + UPDATED_CONTEXT);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByContextIsNotEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where context not equals to DEFAULT_CONTEXT
        defaultUndoLogShouldNotBeFound("context.notEquals=" + DEFAULT_CONTEXT);

        // Get all the undoLogList where context not equals to UPDATED_CONTEXT
        defaultUndoLogShouldBeFound("context.notEquals=" + UPDATED_CONTEXT);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByContextIsInShouldWork() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where context in DEFAULT_CONTEXT or UPDATED_CONTEXT
        defaultUndoLogShouldBeFound("context.in=" + DEFAULT_CONTEXT + "," + UPDATED_CONTEXT);

        // Get all the undoLogList where context equals to UPDATED_CONTEXT
        defaultUndoLogShouldNotBeFound("context.in=" + UPDATED_CONTEXT);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByContextIsNullOrNotNull() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where context is not null
        defaultUndoLogShouldBeFound("context.specified=true");

        // Get all the undoLogList where context is null
        defaultUndoLogShouldNotBeFound("context.specified=false");
    }
                @Test
    @Transactional
    public void getAllUndoLogsByContextContainsSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where context contains DEFAULT_CONTEXT
        defaultUndoLogShouldBeFound("context.contains=" + DEFAULT_CONTEXT);

        // Get all the undoLogList where context contains UPDATED_CONTEXT
        defaultUndoLogShouldNotBeFound("context.contains=" + UPDATED_CONTEXT);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByContextNotContainsSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where context does not contain DEFAULT_CONTEXT
        defaultUndoLogShouldNotBeFound("context.doesNotContain=" + DEFAULT_CONTEXT);

        // Get all the undoLogList where context does not contain UPDATED_CONTEXT
        defaultUndoLogShouldBeFound("context.doesNotContain=" + UPDATED_CONTEXT);
    }


    @Test
    @Transactional
    public void getAllUndoLogsByLogStatusIsEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logStatus equals to DEFAULT_LOG_STATUS
        defaultUndoLogShouldBeFound("logStatus.equals=" + DEFAULT_LOG_STATUS);

        // Get all the undoLogList where logStatus equals to UPDATED_LOG_STATUS
        defaultUndoLogShouldNotBeFound("logStatus.equals=" + UPDATED_LOG_STATUS);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogStatusIsNotEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logStatus not equals to DEFAULT_LOG_STATUS
        defaultUndoLogShouldNotBeFound("logStatus.notEquals=" + DEFAULT_LOG_STATUS);

        // Get all the undoLogList where logStatus not equals to UPDATED_LOG_STATUS
        defaultUndoLogShouldBeFound("logStatus.notEquals=" + UPDATED_LOG_STATUS);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogStatusIsInShouldWork() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logStatus in DEFAULT_LOG_STATUS or UPDATED_LOG_STATUS
        defaultUndoLogShouldBeFound("logStatus.in=" + DEFAULT_LOG_STATUS + "," + UPDATED_LOG_STATUS);

        // Get all the undoLogList where logStatus equals to UPDATED_LOG_STATUS
        defaultUndoLogShouldNotBeFound("logStatus.in=" + UPDATED_LOG_STATUS);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogStatusIsNullOrNotNull() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logStatus is not null
        defaultUndoLogShouldBeFound("logStatus.specified=true");

        // Get all the undoLogList where logStatus is null
        defaultUndoLogShouldNotBeFound("logStatus.specified=false");
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogCreatedIsEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logCreated equals to DEFAULT_LOG_CREATED
        defaultUndoLogShouldBeFound("logCreated.equals=" + DEFAULT_LOG_CREATED);

        // Get all the undoLogList where logCreated equals to UPDATED_LOG_CREATED
        defaultUndoLogShouldNotBeFound("logCreated.equals=" + UPDATED_LOG_CREATED);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogCreatedIsNotEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logCreated not equals to DEFAULT_LOG_CREATED
        defaultUndoLogShouldNotBeFound("logCreated.notEquals=" + DEFAULT_LOG_CREATED);

        // Get all the undoLogList where logCreated not equals to UPDATED_LOG_CREATED
        defaultUndoLogShouldBeFound("logCreated.notEquals=" + UPDATED_LOG_CREATED);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogCreatedIsInShouldWork() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logCreated in DEFAULT_LOG_CREATED or UPDATED_LOG_CREATED
        defaultUndoLogShouldBeFound("logCreated.in=" + DEFAULT_LOG_CREATED + "," + UPDATED_LOG_CREATED);

        // Get all the undoLogList where logCreated equals to UPDATED_LOG_CREATED
        defaultUndoLogShouldNotBeFound("logCreated.in=" + UPDATED_LOG_CREATED);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogCreatedIsNullOrNotNull() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logCreated is not null
        defaultUndoLogShouldBeFound("logCreated.specified=true");

        // Get all the undoLogList where logCreated is null
        defaultUndoLogShouldNotBeFound("logCreated.specified=false");
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogModifiedIsEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logModified equals to DEFAULT_LOG_MODIFIED
        defaultUndoLogShouldBeFound("logModified.equals=" + DEFAULT_LOG_MODIFIED);

        // Get all the undoLogList where logModified equals to UPDATED_LOG_MODIFIED
        defaultUndoLogShouldNotBeFound("logModified.equals=" + UPDATED_LOG_MODIFIED);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogModifiedIsNotEqualToSomething() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logModified not equals to DEFAULT_LOG_MODIFIED
        defaultUndoLogShouldNotBeFound("logModified.notEquals=" + DEFAULT_LOG_MODIFIED);

        // Get all the undoLogList where logModified not equals to UPDATED_LOG_MODIFIED
        defaultUndoLogShouldBeFound("logModified.notEquals=" + UPDATED_LOG_MODIFIED);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogModifiedIsInShouldWork() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logModified in DEFAULT_LOG_MODIFIED or UPDATED_LOG_MODIFIED
        defaultUndoLogShouldBeFound("logModified.in=" + DEFAULT_LOG_MODIFIED + "," + UPDATED_LOG_MODIFIED);

        // Get all the undoLogList where logModified equals to UPDATED_LOG_MODIFIED
        defaultUndoLogShouldNotBeFound("logModified.in=" + UPDATED_LOG_MODIFIED);
    }

    @Test
    @Transactional
    public void getAllUndoLogsByLogModifiedIsNullOrNotNull() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        // Get all the undoLogList where logModified is not null
        defaultUndoLogShouldBeFound("logModified.specified=true");

        // Get all the undoLogList where logModified is null
        defaultUndoLogShouldNotBeFound("logModified.specified=false");
    }
    /**
     * Executes the search, and checks that the default entity is returned.
     */
    private void defaultUndoLogShouldBeFound(String filter) throws Exception {
        restUndoLogMockMvc.perform(get("/api/undo-logs?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(undoLog.getId().intValue())))
            .andExpect(jsonPath("$.[*].branchId").value(hasItem(DEFAULT_BRANCH_ID.intValue())))
            .andExpect(jsonPath("$.[*].xid").value(hasItem(DEFAULT_XID)))
            .andExpect(jsonPath("$.[*].context").value(hasItem(DEFAULT_CONTEXT)))
            .andExpect(jsonPath("$.[*].rollbackInfoContentType").value(hasItem(DEFAULT_ROLLBACK_INFO_CONTENT_TYPE)))
            .andExpect(jsonPath("$.[*].rollbackInfo").value(hasItem(Base64Utils.encodeToString(DEFAULT_ROLLBACK_INFO))))
            .andExpect(jsonPath("$.[*].logStatus").value(hasItem(DEFAULT_LOG_STATUS.booleanValue())))
            .andExpect(jsonPath("$.[*].logCreated").value(hasItem(DEFAULT_LOG_CREATED.toString())))
            .andExpect(jsonPath("$.[*].logModified").value(hasItem(DEFAULT_LOG_MODIFIED.toString())));

        // Check, that the count call also returns 1
        restUndoLogMockMvc.perform(get("/api/undo-logs/count?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(content().string("1"));
    }

    /**
     * Executes the search, and checks that the default entity is not returned.
     */
    private void defaultUndoLogShouldNotBeFound(String filter) throws Exception {
        restUndoLogMockMvc.perform(get("/api/undo-logs?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$").isArray())
            .andExpect(jsonPath("$").isEmpty());

        // Check, that the count call also returns 0
        restUndoLogMockMvc.perform(get("/api/undo-logs/count?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(content().string("0"));
    }

    @Test
    @Transactional
    public void getNonExistingUndoLog() throws Exception {
        // Get the undoLog
        restUndoLogMockMvc.perform(get("/api/undo-logs/{id}", Long.MAX_VALUE))
            .andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    public void updateUndoLog() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        int databaseSizeBeforeUpdate = undoLogRepository.findAll().size();

        // Update the undoLog
        UndoLog updatedUndoLog = undoLogRepository.findById(undoLog.getId()).get();
        // Disconnect from session so that the updates on updatedUndoLog are not directly saved in db
        em.detach(updatedUndoLog);
        updatedUndoLog
            .branchId(UPDATED_BRANCH_ID)
            .xid(UPDATED_XID)
            .context(UPDATED_CONTEXT)
            .rollbackInfo(UPDATED_ROLLBACK_INFO)
            .rollbackInfoContentType(UPDATED_ROLLBACK_INFO_CONTENT_TYPE)
            .logStatus(UPDATED_LOG_STATUS)
            .logCreated(UPDATED_LOG_CREATED)
            .logModified(UPDATED_LOG_MODIFIED);
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(updatedUndoLog);

        restUndoLogMockMvc.perform(put("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isOk());

        // Validate the UndoLog in the database
        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeUpdate);
        UndoLog testUndoLog = undoLogList.get(undoLogList.size() - 1);
        assertThat(testUndoLog.getBranchId()).isEqualTo(UPDATED_BRANCH_ID);
        assertThat(testUndoLog.getXid()).isEqualTo(UPDATED_XID);
        assertThat(testUndoLog.getContext()).isEqualTo(UPDATED_CONTEXT);
        assertThat(testUndoLog.getRollbackInfo()).isEqualTo(UPDATED_ROLLBACK_INFO);
        assertThat(testUndoLog.getRollbackInfoContentType()).isEqualTo(UPDATED_ROLLBACK_INFO_CONTENT_TYPE);
        assertThat(testUndoLog.isLogStatus()).isEqualTo(UPDATED_LOG_STATUS);
        assertThat(testUndoLog.getLogCreated()).isEqualTo(UPDATED_LOG_CREATED);
        assertThat(testUndoLog.getLogModified()).isEqualTo(UPDATED_LOG_MODIFIED);
    }

    @Test
    @Transactional
    public void updateNonExistingUndoLog() throws Exception {
        int databaseSizeBeforeUpdate = undoLogRepository.findAll().size();

        // Create the UndoLog
        UndoLogDTO undoLogDTO = undoLogMapper.toDto(undoLog);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restUndoLogMockMvc.perform(put("/api/undo-logs").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(undoLogDTO)))
            .andExpect(status().isBadRequest());

        // Validate the UndoLog in the database
        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    public void deleteUndoLog() throws Exception {
        // Initialize the database
        undoLogRepository.saveAndFlush(undoLog);

        int databaseSizeBeforeDelete = undoLogRepository.findAll().size();

        // Delete the undoLog
        restUndoLogMockMvc.perform(delete("/api/undo-logs/{id}", undoLog.getId()).with(csrf())
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<UndoLog> undoLogList = undoLogRepository.findAll();
        assertThat(undoLogList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
